package com.mjlf.mqzook.controller;

import com.alibaba.fastjson.JSONObject;
import com.mjlf.mqzook.entity.ZkTopic;
import com.mjlf.mqzook.inter.TopicFactroy;
import com.mjlf.mqzook.server.TopicServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName ServerRegistryController
 * @Author mjlft
 * @Date 2020/2/18 11:08
 * @Version 1.0
 * @Description topic controller
 */
@RestController
public class TopicController {

    Logger logger = LoggerFactory.getLogger(TopicController.class);

    @Autowired
    private TopicServer topicServer;

    @PostMapping("/createTopic")
    public JSONObject createTopic(@RequestBody ZkTopic topic){
        return topicServer.createTopic(topic);
    }

    @GetMapping("/getTopics")
    public JSONObject getTopics(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 200);
        jsonObject.put("data",JSONObject.toJSONString(TopicFactroy.TOPICS));
        return jsonObject;
    }

    @PostMapping("/releader")
    public JSONObject createTopic(@RequestBody JSONObject jsonObject){
        String host = jsonObject.getString("host");
        Integer port = jsonObject.getInteger("port");
        return topicServer.releader(host,port);
    }
}
